package com.manager.core.module.manager.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.manager.commons.base.DataGrid;
import com.manager.commons.mybatis.dao.GenericDao;
import com.manager.commons.mybatis.service.GenericService;
import com.manager.core.module.manager.dao.PlatformAreaDao;
import com.manager.core.module.manager.entity.ComboTreeList;
import com.manager.core.module.manager.entity.PlatformArea;
import com.manager.core.module.manager.entity.article.GeneralTopicCategory;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * PlatformAreaService
 * 基础配置-区域
 *
 * @author manager
 * @version 1.0
 */
@Service
public class PlatformAreaService extends GenericService<PlatformArea> {

    @Autowired
    private PlatformAreaDao jdbcDao;

    @Override
    public GenericDao<PlatformArea> getDao() {
        return jdbcDao;
    }

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    public DataGrid queryPage(Map<String, Object> params) {
        try {
            if (null != params) {
                int count = jdbcDao.countMap(params);
                List<PlatformArea> list = jdbcDao.queryMap(params);
                queryChildren(list, params);
                return new DataGrid(count, list);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return new DataGrid(0, new ArrayList<GeneralTopicCategory>());
    }

    public List<Map<String, Object>> querySelect(String params) {
        Map<String, Object> map = new HashMap<>();
        if (null != params && !"0".equals(params)) {
            map.put("parent_code", params);
        }

        return jdbcDao.querySelect(map);

    }


    private void queryChildren(List<PlatformArea> list, Map<String, Object> params) {
        for (PlatformArea gtc : list) {
            params.put("parent_code", gtc.getCode());
            List<PlatformArea> l = jdbcDao.queryMap(params);
            if (l != null && !l.isEmpty()) {
                gtc.setChildren(l);
                queryChildren(l, params);
            }
        }
    }

    public List<ComboTreeList> queryTreeList() {
        return jdbcDao.queryTree();
    }

    public int importJson() {
        //通过JSON文件加载模块组
        try {
            InputStream in = PlatformAreaService.class.getClassLoader().getResourceAsStream("citys.json");
            logger.debug("----------加载配置文件成功[citys.json]----------");
            String str = IOUtils.toString(in, StandardCharsets.UTF_8);
            JSONArray jsonArray = JSONArray.parseArray(str);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                Map<String, Object> province = new HashMap<>();
                String provinceCode = obj.getString("provinceCode");
                province.put("code", provinceCode);
                province.put("name", obj.getString("provinceName"));
                province.put("parentCode", null);
                province.put("level", 1);
                province.put("seq", i);
                jdbcDao.insertMap(province);
                JSONArray cityArray = obj.getJSONArray("mallCityList");
                for (int j = 0; j < cityArray.size(); j++) {
                    JSONObject city = cityArray.getJSONObject(j);
                    Map<String, Object> cityMap = new HashMap<>();
                    String cityCode = city.getString("cityCode");
                    cityMap.put("code", cityCode);
                    cityMap.put("name", city.getString("cityName"));
                    cityMap.put("parentCode", provinceCode);
                    cityMap.put("level", 2);
                    cityMap.put("seq", j);
                    jdbcDao.insertMap(cityMap);
                    JSONArray areaArray = city.getJSONArray("mallAreaList");
                    for (int f = 0; f < areaArray.size(); f++) {
                        JSONObject area = areaArray.getJSONObject(f);
                        Map<String, Object> areaMap = new HashMap<>();
                        areaMap.put("code", area.getString("areaCode"));
                        areaMap.put("name", area.getString("areaName"));
                        areaMap.put("parentCode", cityCode);
                        areaMap.put("seq", f);
                        areaMap.put("level", 3);
                        jdbcDao.insertMap(areaMap);
                    }
                }
            }
            return 1;
        } catch (IOException e) {
        }
        return 0;
    }

}
